package com.base.cn.platform.os.controller.qa;

import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.service.course.qa.qareply.QaReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 问答回复接口
 *
 * @author s.li
 * @create 2018-09-06-18:21
 */
@RestController
@RequestMapping("/api/qa")
public class ApiQaReplyController extends ApiBaseController {

    @Autowired
    private QaReplyService qaReplyService;

    /**
     * 获取评论下的所有子评论列表（分页）
     * @param replyId 评论ID
     * @param currentPage 页码
     * @return Map<String,Object>
     */
    @RequestMapping("/getChildReplyListByReplyId")
    public Map<String,Object> getChildReplyListByReplyId(@RequestParam("replyId") BigDecimal replyId,
                                                         @RequestParam(value = "currentPage",required = false,defaultValue = "0") int currentPage){
        Map<String,Object> params = new HashMap<>();
        params.put("parentId",replyId);
        Map<String,Object> dataMap = qaReplyService.findQaReplyListPage(currentPage,15,params,true,false);
        return ResultUtil.SUCCESS(dataMap);
    }

    /**
     * 问答下的评论列表
     * @param currentPage 页码
     * @param questionId 问答ID
     * @return Map<String,Object>
     */
    @RequestMapping("/queryReplyListByQaId")
    public Map<String,Object> queryReplyListByQaId(@RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage,
                                                   @RequestParam("questionId") BigDecimal questionId){
        Map<String,Object> params = new HashMap<>();
        params.put("questionId",questionId);
        params.put("status",1);
        params.put("parentId",0);
        Map<String, Object> dataMap = qaReplyService.findQaReplyListPage(currentPage, 10, params, true, true);
        return ResultUtil.SUCCESS(dataMap);
    }

    /**
     * 保存问答回复
     * @param userId 评论用户ID
     * @param questionId 被评论问答ID
     * @param replyContext 评论内容
     * @param parentId 父级ID
     * @param toUserId 被评论用户ID
     * @return Map<String,Object>
     */
    @RequestMapping("/saveQuestionReply")
    public Map<String,Object> saveQuestionReply(@RequestParam("userId") BigDecimal userId,
                                                @RequestParam("questionId") BigDecimal questionId,
                                                @RequestParam("replyContext") String replyContext,
                                                @RequestParam(value = "parentId",required = false,defaultValue = "0") BigDecimal parentId,
                                                @RequestParam(value = "toUserId",required = false,defaultValue = "0") BigDecimal toUserId){
        Map<String,Object> questionReply = new HashMap<>();
        questionReply.put("userId",userId);
        questionReply.put("questionId",questionId);
        questionReply.put("replyContext",replyContext);
        questionReply.put("parentId",parentId);
        questionReply.put("toUserId",toUserId);
        return qaReplyService.saveQaReply(questionReply);
    }
}
